<template>
  <div class="backgroundUser">
    <!-- 搜索框 -->
    <div class="header">
      <el-input
        placeholder="请输入名称"
        v-model="param.baseKey"
        class="input-with-select"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="searchUser"
        ></el-button>
      </el-input>
      <el-button type="primary" icon="el-icon-plus" @click="addUser"
        >新增用户</el-button
      >
    </div>

    <!-- 表格 -->
    <template>
      <el-table
        :data="tableData"
        style="width: 100%"
        class="table"
        height="650"
      >
        <el-table-column prop="userId" label="ID" width="180">
        </el-table-column>
        <el-table-column prop="nickName" label="登录名" width="180">
        </el-table-column>
        <el-table-column prop="roleName" label="用户角色"> </el-table-column>
        <el-table-column prop="branchCompany.location" label="所属公司">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="editUser(scope.row)">编辑</el-button>
            <i>|</i>
            <el-button type="text" @click="resetPassword(scope.row)"
              >重置密码</el-button
            >
            <i>|</i>
            <el-button type="text" @click="delectUsers(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </template>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="param.page"
      :page-sizes="[1, 3, 5, 10]"
      :page-size="param.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalnum"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 搜索框
      input3: '',
      // 表格
      tableData: [],
      // 分页
      // currentPage: 1,
      // 总条数
      totalnum: 0,
      param: { baseKey: '', page: 1, size: 3 }
    }
  },
  mounted () {
    this.getUsers()
  },
  methods: {
    // 获取初始化数据
    async getUsers () {
      const res = await this.$http.getuser(this.param)
      this.tableData = res.data.data
      this.totalnum = res.parameters.totalElements
      console.log(res)
      // console.log(res.data.data)
    },
    // 分页
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.param.size = val
      this.getUsers()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.param.page = val
      this.getUsers()
    },

    // 查询用户
    searchUser () {
      // console.log(111)
      this.getUsers()
    },

    // 编辑用户
    async editUser (item) {
      // 跳转页面
      this.$router.push({
        path: '/editUser',
        query: { item: item }
      })
    },
    // 新增用户
    async addUser () {
      this.$router.push({
        path: '/addUser'
      })
    },

    // 删除用户
    async delectUsers (item) {
      console.log(item)
      // 提示信息
      const isclose = await this.$confirm('此操作将永久删除该用户,是否继续？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {})
      // 如果取消则直接false
      if (isclose != 'confirm') return this.$message.info('取消删除')
      // 请求参数
      const data = await this.$http.deleteUser({ userId: item.userId })
      console.log(data)
      // console.log(item.usersId)
      this.getUsers()
    },
    // 重置密码
    async resetPassword (item) {
      this.$confirm('是否对id为[' + item.userId + ']的用户重置密码?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await this.$http.resetPassword({ usersId: item.usersId })
          this.$message({
            type: 'success',
            message: '重置成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消重置'
          })
        })
    }
  }
}
</script>

<style lang="less" scoped>
.backgroundUser {
  i {
    font-style: normal;
    padding: 0 6px;
    color: #e0dad7;
  }
  a {
    color: #7db1ff;
  }
  .header {
    display: flex;
    justify-content: space-between;
    .input-with-select {
      width: 250px;
    }
  }
  .table {
    margin-bottom: 18px;
    /deep/.cell {
      text-align: center;
      line-height: 44px;
      height: 44px;
    }
  }
}
</style>
